<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $promotionid 
 * @property string $mid1 
 * @property string $imei 
 * @property string $oaid 
 * @property string $aid 
 * @property string $androidid 
 * @property string $os 
 * @property string $TIMESTAMP 
 * @property string $callback 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class OcpcDouyin extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_douyin';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'promotionid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];


    /**
     * @param array $params
     * @return int
     */
    static function addLog(array $params = []): int
    {
        $oaid = $params['oaid'] ?? '';

        $data = [
            'promotionid'      => $params['promotionid'] ?? '',
            'mid1'        => $params['mid1'] ?? '',
            'oaid'        => $oaid,
            'imei'   => $params['imei'] ?? '',
            'aid'     => $params['aid'] ?? '',
            'androidid'         => $params['androidid'] ?? '',
            'os'          => $params['os'] ?? '',
            'TIMESTAMP'  => $params['TIMESTAMP'] ?? '',
            'callback'       => $params['callback'] ?? '',
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ];

        return self::insertGetId($data);
    }

    public static function getData()
    {

    }

    /**
     * 根据oaid查询是否存在
     * @param string $oaid
     * @return bool
     */
    public static function existsOaid(string $oaid)
    {
        if (empty($oaid)) {
            return false;
        }

        return self::where('oaid', $oaid)
//            ->where('channel_id', $channel)
            ->exists();
    }

    /**
     * 根据imei查询是否存在
     * @param string $imei
     * @return bool
     */
    public static function existImei(string $imei)
    {
        if (empty($imei)) {
            return false;
        }

        return self::where([
            'imei'    => $imei,
//            'channel_id' => $channel
        ])->exists();
    }


    /**
     * 处理查询
     * @param $info
     * @return array|\Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object
     */
    public static function handleSearch($info)
    {
        //oaid 查询
        if (isset($info['oaid']) && $info['oaid']) {
            $res = self::where('oaid', $info['oaid'])
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'oaid','imei','callback']);

            if ($res) {
                return $res->toArray();
            }
        }

        if (isset($info['imei']) && $info['imei']) {
            $res = self::where('imei', md5($info['imei']))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'imei','oaid','callback']);

            if ($res) {
                return $res->toArray();
            }
        }

        return [];
    }
}